package com.leilei;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lei
 * @version 1.0
 * @date 2021/4/10 21:32
 * @desc filter 过滤算子
 */
public class FilterOperator {
    public static void main(String[] args) throws Exception {
        List<Integer> str1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        List<Integer> dataList = str1.stream().filter(e -> e > 3).collect(Collectors.toList());
        // [4, 5, 6, 7, 8]
        System.out.println(dataList);


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 设置并行度为1 （1个线程执行，以便观察）
        env.setParallelism(1);
        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);
        // 加载数据源
        DataStreamSource<Integer> stream = env.fromCollection(str1);
        // 使用filter算子
        SingleOutputStreamOperator<Integer> source = stream.filter(new FilterFunction<Integer>() {
            @Override
            public boolean filter(Integer value) throws Exception {
                return value > 3;
            }
        });
        // 4 5 6 7 8
        source.print();
        env.execute();
    }
}
